﻿//---------------------------------------------------------------------------
// 
// File: Converters.cs
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//---------------------------------------------------------------------------

namespace HTMLConverter
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using HTMLConverter.XamlConverters;

    internal class Converters : IEnumerable<IConverter>
    {
        static Converters collection;
        public static Converters Collection
        {
            get
            {
                if (collection == null)
                {
                    collection = new Converters();
                }

                return collection;
            }
        }

        Dictionary<Tag, IConverter> converters;

        private Converters()
        {
            converters = new Dictionary<Tag, IConverter>();

            Add(new SectionConverter());
            Add(new ParagraphConverter());
            Add(new SpanConverter());
            Add(new RunConverter());
            Add(new HyperlinkConverter());
            Add(new ImageConverter());
            Add(new ItalicConverter());
            Add(new TitleConverter());
            Add(new UnderlineConverter());
            Add(new BoldConverter());
            Add(new ListConverter());
            Add(new ListItemConverter());
            Add(new TableConverter());
            Add(new TableRowConverter());
            Add(new TableCellConverter());
        }

        int Add(IConverter converter)
        {
            if (converter != null && converters.ContainsKey(converter.Type) == false)
            {
                converters.Add(converter.Type, converter);
            }

            return converters.Count;
        }

        public IConverter this[Tag type]
        {
            get
            {
                if (converters.ContainsKey(type))
                {
                    return converters[type];
                }

                throw new System.NotSupportedException(string.Format("Type of {0:G}", type));
            }
        }

        #region IEnumerable<IConverter> Members
        public IEnumerator<IConverter> GetEnumerator()
        {
            return converters.Select(s => s.Value).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion
    }
}
